#!/bin/sh
###############################################################################
##  Author    : xuezhoyi
##  Name      : edw_batch_end.sh
##  Functions : 跑批结束
##  Purpose   : 
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2017-05-22  xuezhouyi        1. CREATED THIS SHELL.
##  2.0      2018-11-11  薛周毅           1.更新表【EDW_BATCH_DATE】的结束时间
##                                        2.在表【EDW_BATCH_DATE】中初始化下一个批次的数据
###############################################################################

. ${0%`basename ${0}`}edw_parm.sh
. ${0%`basename ${0}`}edw_func.sh


V_DATA_DATE=$(grep "#V_DATA_DATE#" ${V_PARM_FILE} | awk -F'=' '{print $2}')
V_FREQUENCY=$(grep "#V_FREQUENCY#" ${V_PARM_FILE} | awk -F'=' '{print $2}')
# Define basic parm
V_SCRIPT_NAME=$(basename ${0} | cut -d'.' -f1)
V_OUT_LOGS="${V_SHELL_LOGS}/${V_DATA_DATE}/${V_SCRIPT_NAME}_${V_FREQUENCY}.log"

# Define the output flow
exec 4>&1               # screen output
exec 3>>${V_OUT_LOGS}   # script output
exec 2>&3               # error output
exec 1>&3               # standard output

BEGIN
LOGGER "INFO" "Today batch date is ${V_DATA_DATE}."

V_QUERY="SELECT START_TIME FROM ETL.EDW_BATCH_DATE WHERE DATA_DATE='${V_DATA_DATE}' AND STATUS='RUN'"
START_TIME=$(${V_RUN_DB} -q "${V_QUERY}") || exit 1
END_TIME=$(NOW)
RUN_TIME=$(DIF_TIME "${START_TIME}" "${END_TIME}")
V_QUERY="UPDATE ETL.EDW_BATCH_DATE SET STATUS='DONE',END_TIME='${END_TIME}',RUN_TIME='${RUN_TIME}' WHERE DATA_DATE='${V_DATA_DATE}' AND STATUS='RUN'"
${V_RUN_DB} -q "${V_QUERY}" || exit 2

END
exit 0
